package org.geomesa.example.data;

import org.geotools.data.Query;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.filter.text.ecql.ECQL;
import org.locationtech.geomesa.utils.interop.SimpleFeatureTypes;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;
import org.geotools.factory.CommonFactoryFinder;
import org.locationtech.jts.geom.*;
import java.util.*;

public class PolygonData implements TutorialData {

    private SimpleFeatureType sft = null;
    private List<SimpleFeature> features = null;
    private List<Query> queries = null;
    private Filter subsetFilter = null;

    @Override
    public String getTypeName() {
        return "custom_polygon";
    }

    @Override
    public SimpleFeatureType getSimpleFeatureType() {
        if (sft == null) {
            String spec = "geom:Polygon:srid=4326,name:String";
            sft = SimpleFeatureTypes.createType(getTypeName(), spec);
        }
        return sft;
    }

    @Override
    public List<SimpleFeature> getTestData() {
        if (features == null) {
            features = new ArrayList<>();
            GeometryFactory geometryFactory = new GeometryFactory();

            // 创建四个矩形Polygon
            Polygon rect1 = geometryFactory.createPolygon(new Coordinate[]{
                new Coordinate(130, 28),
                new Coordinate(135, 28),
                new Coordinate(135, 22),
                new Coordinate(130, 22),
                new Coordinate(130, 28)
            });
            Polygon rect2 = geometryFactory.createPolygon(new Coordinate[]{
                new Coordinate(140, 30),
                new Coordinate(145, 30),
                new Coordinate(145, 25),
                new Coordinate(140, 25),
                new Coordinate(140, 30)
            });
            Polygon rect3 = geometryFactory.createPolygon(new Coordinate[]{
                new Coordinate(120, 35),
                new Coordinate(125, 35),
                new Coordinate(125, 32),
                new Coordinate(120, 32),
                new Coordinate(120, 35)
            });
            Polygon rect4 = geometryFactory.createPolygon(new Coordinate[]{
                new Coordinate(110, 20),
                new Coordinate(115, 20),
                new Coordinate(115, 15),
                new Coordinate(110, 15),
                new Coordinate(110, 20)
            });

            SimpleFeatureBuilder builder = new SimpleFeatureBuilder(getSimpleFeatureType());
            builder.set("geom", rect1);
            builder.set("name", "Rectangle1");
            features.add(builder.buildFeature("rect1"));

            builder.set("geom", rect2);
            builder.set("name", "Rectangle2");
            features.add(builder.buildFeature("rect2"));

            builder.set("geom", rect3);
            builder.set("name", "Rectangle3");
            features.add(builder.buildFeature("rect3"));

            builder.set("geom", rect4);
            builder.set("name", "Rectangle4");
            features.add(builder.buildFeature("rect4"));
        }
        return features;
    }

    @Override
    public List<Query> getTestQueries() {
        if (queries == null) {
            try {
                queries = new ArrayList<>();
                // 查询包含某一区域的Polygon
                String cql = "BBOX(geom, 130, 22, 145, 30)";
                queries.add(new Query(getTypeName(), ECQL.toFilter(cql)));
            } catch (Exception e) {
                throw new RuntimeException("Error creating test queries", e);
            }
        }
        return queries;
    }

    @Override
    public Filter getSubsetFilter() {
        if (subsetFilter == null) {
            FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
            subsetFilter = ff.bbox("geom", 130, 22, 145, 30, "EPSG:4326");
        }
        return subsetFilter;
    }
}